//
//  ARGlobalInterface.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/22.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARGlobalInterface.h"
#import "NSString+MD5Addition.h"

@implementation ARGlobalInterface

+ (NSString *)findRecomentCacheURL
{
    NSString *path = NSHomeDirectory();
    
    NSString *cachePath = [[path stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"recommend.json"];
    
    NSString *appPath =[[NSBundle mainBundle] pathForResource:@"recommend" ofType:@"json"];
    
   // appPath = [appPath stringByAppendingPathComponent:@"recommend.json"];
    
   // NSLog(@"%@",appPath);
    
    return appPath;
    
    

    

}

+ (NSString *)channelSensesServerURLWithID:(NSString *)index
{
    return [NSString stringWithFormat:@"https://v2.same.com/channel/%@/senses",index];
}

+ (NSString *)channelSensesCacheURLWithID:(NSString *)index
{
    NSString *path = NSHomeDirectory();
    
    NSString *cachePath = [[path stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"channelSenses%@",index]];
    return cachePath;
}

+ (NSString *)channelDetailServerURLWithID:(NSString *)index
{
    return [NSString stringWithFormat:@"https://v2.same.com/channel/%@/detail",index];
}

+ (NSString *)channelDetailChacheURLWithID:(NSString *)index
{
    NSString *path = NSHomeDirectory();
    
    NSString *cachePath = [[path stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"channelDetail%@",index]];
    return cachePath;

}

+ (NSString *)channelOnlineCountServerURLWithID:(NSString *)index
{
    return [NSString stringWithFormat:@"https://api-im-prd-xs.ohsame.com//imchannel/oluserscount?channel_id=%@",index];
}


+ (NSString *)channelMoreSensesServerURLWithID:(NSString *)nextURL
{
    return [NSString stringWithFormat:@"https://v2.same.com%@",nextURL];
}

+ (NSString *) findLastCacheURL
{
    NSString *path = NSHomeDirectory();
    
    NSString *cachePath = [[path stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"findLast"]];
    return cachePath;

}
+ (NSString *) findLastServerURL
{
    return @"https://v2.same.com/latest/channels";
}
+ (NSString *) findMoreServerURLWithID:(NSString *) nextURL
{
    return [NSString stringWithFormat:@"https://v2.same.com%@",nextURL];
}


+ (NSString *) searchWithCate:(NSString *)cate WithKey:(NSString *)key
{
    
    NSString *unicodeStr = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *url = [NSString stringWithFormat:@"http://search.ohsame.com/s.php?cate=%@&format=json&keyword=%@&type=1",cate,unicodeStr];
    
    return url;

}


+ (NSString *) musicTempCachePathWithMusicPath:(NSString *) path
{
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *md5URL = [path stringFromMD5];
    NSString *filePath = [NSString stringWithFormat:@"%@/musictemp/%@.mp3",docDirPath,md5URL];
    NSString *filesPath = [NSString stringWithFormat:@"%@/musictemp",docDirPath];
    
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:filesPath] == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filesPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return filePath;
    

}

+ (NSArray *) tempMusicInCache
{
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filesPath = [NSString stringWithFormat:@"%@/musictemp",docDirPath];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:filesPath] == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filesPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    
    NSFileManager * fm = [NSFileManager defaultManager];
    //浅度遍历
    NSError * error = nil;
    NSArray * itemNames = [fm contentsOfDirectoryAtPath:filesPath error:&error];
    if(error){
        NSLog(@"%@", error);
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *path in itemNames) {
        NSString *filePath = [NSString stringWithFormat:@"%@/musictemp/%@",docDirPath,path];
        
        [array addObject:filePath];
    }
    
    
    return array;


}

+ (void) removeAllTempMusic
{
    
    NSString *extension = @"mp3";
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filesPath = [NSString stringWithFormat:@"%@/musictemp",docDirPath];
    
    NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filesPath error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {
        
        if ([[filename pathExtension] isEqualToString:extension]) {
            
            [[NSFileManager defaultManager] removeItemAtPath:[filesPath stringByAppendingPathComponent:filename] error:NULL];
        }
    }


}

+ (NSString *) audioWAVTempCachePath{
    
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/audiotemp.wav",docDirPath];

    
    return filePath;
}

+ (NSString *) audioAMRTempCachePath{
    
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/audiotemp.amr",docDirPath];
    
    
    return filePath;
}
@end
